/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.mzk.kramerius.cleaner;

import cz.mzk.kramerius.commons.PropertyLoader;
import cz.mzk.kramerius.fedoraaccess.FedoraConnector;
import cz.mzk.kramerius.fedoraaccess.FedoraServer;
import cz.mzk.kramerius.fedoraaccess.rest.RESTConnector;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.dom4j.DocumentException;

/**
 *
 * @author Martin Řehánek (rehan at mzk.cz)
 */
public class Main {

    private static final String FEDORA_URL_PROPERTY = "Fedora.url";
    private static final String FEDORA_LOGIN_PROPERTY = "Fedora.login";
    private static final String FEDORA_PASSWORD_PROPERTY = "Fedora.password";
    private static FedoraConnector con;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        //init();

        //MonographRemover monRemover = new MonographRemover(con);
        //monRemover.removeMonograph("uuid:2f7b4316-ed09-11df-9847-0050569e4b0b");

//        PeriodicalRemover perRemover = new PeriodicalRemover(con);
//        perRemover.removePeriodical("uuid:e8da5405-f380-11df-8d57-0050569e4b0b");

        String pid = "uuid:644c2243-f4f5-11df-b0f1-0050569d679d";
        //String pid = "uuid:19ab1a17-f4f6-11df-b0f1-0050569d679d";


        System.err.println("KrameriusCleaner: No cli yet");

        //saveRelsExt(pid);
        //updateRelsExt(pid);
        //purgeObjects();
    }

    private static void saveRelsExt(String pid) {
        RelsExtUpdater relsExtUpdater = new RelsExtUpdater(con);
        relsExtUpdater.saveRelsExtToFile(pid, pidToFile(pid));
    }

    private static void updateRelsExt(String pid) {
        try {
            RelsExtUpdater relsExtUpdater = new RelsExtUpdater(con);
            relsExtUpdater.updateRelsExtFromFile(pid, pidToFile(pid));
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (DocumentException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private static void purgeObjects() {
        List<String> pidList = new ArrayList<String>();
        pidList.add("uuid:ac7e89a8-980f-11e0-9644-0050569d679d");
        pidList.add("uuid:014d32b3-9810-11e0-9644-0050569d679d");
        pidList.add("uuid:5ac42f21-9810-11e0-9644-0050569d679d");


        for (String pid : pidList) {
            con.removeObject(pid, "nadbytecny top-level periodical objekt po spojeni rocniku");
            System.out.println("removed " + pid);
        }
        con.printRepositoryInfo();
    }

    private static void init() {
        String propertiesFile = "/home/rehan/NetBeansProjects/Kramerius4MzkAccess/project.properties";
        try {
            PropertyLoader propLoader = new PropertyLoader(propertiesFile);
            String fedoraUrl = propLoader.loadString(FEDORA_URL_PROPERTY);
            String fedoraLogin = propLoader.loadString(FEDORA_LOGIN_PROPERTY);
            String fedoraPass = propLoader.loadString(FEDORA_PASSWORD_PROPERTY);
            con = new RESTConnector(new FedoraServer(fedoraUrl, fedoraLogin, fedoraPass));
            //con.printRepositoryInfo();
        } catch (Exception e) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, e);
        }
    }

    private static File pidToFile(String pid) {
        String dir = "C:\\Users\\Martin\\Documents\\práce\\MZK\\tmp\\";
        //File outFile = new File(dir.concat("rels_ext").concat(".xml"));
        File file = new File(dir.concat(unprefixString(pid, "uuid:")).concat(".xml"));
        System.out.println("file: " + file.getAbsolutePath());
        return file;
    }

    private static String unprefixString(String sourceString, String prefix) {
        int prefixSize = prefix.length();
        return sourceString.substring(prefixSize);
    }
}
